import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Vector;
import java.util.EventObject;
import java.sql.*;
import module4.*;
import project.*;
import Password.*;
import ManagersArea.ManagersAreaGUIS.*;
import MainFunctions.MainFunctionsGUIS.*;
import Help.*;
import Stock.StockGUIS.*;
import employee.*;
import employee.gui.*;
import client.*;
import client.gui.*;



public class MainFrameGui extends JFrame implements ChangeListener
{
	private JPanel stockP = null,clientP = null,tabbedPanel = null;
	private JPanel mainBaseP = null,stockBaseP = null,clientBaseP = null,empBaseP = null,managerBaseP = null;
	
	private Box statusBar = null;
	private JLabel statusLabel = null,instructLabel = null;
	private JScrollPane scrollPane = null;
	private JTabbedPane mainTab = null;
	private ImageIcon ii = null;
	private JComboBox jcb1 = null, jcb2 = null, jcb3 = null, jcb4 = null;
	private int lastSelectedTab = 0;
	private int lastAccessLevel = 1;
	
	//main Panels
	private ManagersArea mAreaPanel = null;
	private PasswordXXX pDialog;
	private ClientPanel clientsPanel = null;
	private EmpPanel employeePanel = null;
	private MainGUI theMainGUI = null;
	private StockSearchPanel theSearchGUI = null;
	private HelpPanel theHelpPanel = null;
	
	//Connection attributes
	private String myDriver = "com.mysql.jdbc.Driver";
	private String ipOfDB = "localhost";
	private MySQLdriverConnection msc;
	private Connection turtleConn;
	
	//Colours and fonts
	Font tabFont = null;
	
	
	public MainFrameGui() 
	{
		//colours & fonts
	    tabFont = new Font("ButtonFont",Font.BOLD,15);
	
		//Setting up Connection
		try
		{
			msc = new MySQLdriverConnection("root","",ipOfDB,"turtleData");
			turtleConn = msc.getConnection();
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}		
		//*************************************************************************
		
		//setting up main Panels
		statusBar = new Box(BoxLayout.X_AXIS);
		statusLabel = new JLabel("Logged in : ");
		instructLabel = new JLabel("");
		statusBar.add(Box.createHorizontalStrut(10));
		statusBar.add(statusLabel);
		statusBar.add(Box.createHorizontalStrut(480));
		statusBar.add(instructLabel);
		statusBar.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
		
		mAreaPanel = new ManagersArea(turtleConn);
		clientsPanel = new ClientPanel(this,turtleConn);
		employeePanel = new EmpPanel(turtleConn);
		theMainGUI = new MainGUI();
		theSearchGUI = new StockSearchPanel(turtleConn);
		theHelpPanel = new HelpPanel();
		
		
		mainBaseP = new JPanel(new GridLayout(1,1));
		stockBaseP = new JPanel(new GridLayout(1,1));
		clientBaseP = new JPanel(new GridLayout(1,1));
		empBaseP = new JPanel(new GridLayout(1,1));
		managerBaseP = new JPanel(new GridLayout(1,1));
		
		mainBaseP.add(theMainGUI);
		stockBaseP.add(theSearchGUI);
		clientBaseP.add(clientsPanel);
		empBaseP.add(employeePanel);
		managerBaseP.add(mAreaPanel);
		
		//tabbedPanel = new JPanel(new GridLayout());
		mainTab = new JTabbedPane();
		mainTab.addChangeListener(this);
		mainTab.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
		mainTab.setFont(tabFont);
		
		mainTab.addTab("MAIN PROCESSES",mainBaseP);
		mainTab.addTab("STOCK CONTROL",stockBaseP);
		mainTab.addTab("CLIENTS",clientBaseP);
		mainTab.addTab("EMPLOYEES",empBaseP);
		mainTab.addTab("ManagersAREA",managerBaseP);
		mainTab.addTab("",new ImageIcon("pics/Icons/YellowHelp.png"),theHelpPanel);
		
		//tabbedPanel.add(mainTab);
		
		//setup "this" Jpanel
		MyWindowListener mw = new MyWindowListener();
		this.addWindowListener(mw);
		this.getContentPane();
	    this.setSize(900,720);
		this.setLocationRelativeTo(null);
		this.setMinimumSize(new Dimension(900,720));
		this.setLayout(new BorderLayout());
		this.add(mainTab);
		this.add(statusBar,BorderLayout.SOUTH);
	    this.setVisible(true);
	}	
	
	public void stateChanged(ChangeEvent ce)
	{	
		if(mainTab.getSelectedIndex() == 0)
		{
			setStatus("Logged In : "," Select process from main Tab");
			lastSelectedTab = 0;
			System.out.println("in 0gggggg");
			lastAccessLevel = 1;
			pDialog = null;
		}
		if(mainTab.getSelectedIndex() == 1)
		{
			if(((lastSelectedTab != 4) || (lastAccessLevel != 3) || (lastAccessLevel != 1)|| (lastSelectedTab != 5)) && (lastAccessLevel != 3))
			{
				stockBaseP.removeAll();
			    theSearchGUI = new StockSearchPanel(turtleConn);   
			    stockBaseP.add(theSearchGUI);
			    stockBaseP.validate();
			    stockBaseP.repaint();
				String dialogMsg = "      Please enter your password to gain access to Stock" ;	       
				pDialog = new PasswordXXX(this,dialogMsg,turtleConn);  
				 
				if(pDialog.getLogonSuccess() == false)
				{
					mainTab.setSelectedIndex(0);
					setStatus("Logged In : ","");
				}
				else 
				if(theSearchGUI.getAccessLevel()  > pDialog.getAccessLevel())
				{
					JOptionPane.showMessageDialog(this,""+pDialog.getFirstAndLastName()+ " Does not have access to this area!");
					mainTab.setSelectedIndex(0);
					setStatus("Logged In : ","");
				}
				else
				{
					setStatus("Logged In : " +pDialog.getFirstAndLastName(),"Click on \"Main Functions\" tab to LOG OUT");
					lastSelectedTab = 3;
					lastAccessLevel = pDialog.getAccessLevel();
				}
			}//END if	
			if(pDialog != null)
			{
			    lastAccessLevel = pDialog.getAccessLevel();
			    lastSelectedTab = 1;
			}
		}
		if(mainTab.getSelectedIndex() == 2)
		{
			if(((lastSelectedTab != 4) || (lastAccessLevel != 3) || (lastAccessLevel != 1)|| (lastSelectedTab != 5)) && (lastAccessLevel != 3))
			{
			    clientBaseP.removeAll();
			    clientsPanel = new ClientPanel(this,turtleConn);
			    clientBaseP.add(clientsPanel);
			    clientBaseP.validate();
			    clientBaseP.repaint();
				String dialogMsg = "      Please enter your password to gain access to Clients" ;	       
				pDialog = new PasswordXXX(this,dialogMsg,turtleConn);  
				 
				if(pDialog.getLogonSuccess() == false)
				{
					mainTab.setSelectedIndex(0);
					setStatus("Logged In : ","");
				}
				else 
				if(clientsPanel.getAccessLevel()  > pDialog.getAccessLevel())
				{
					JOptionPane.showMessageDialog(this,""+pDialog.getFirstAndLastName()+ " Does not have access to this area!");
					mainTab.setSelectedIndex(0);
					setStatus("Logged In : ","");
				}
				else
				{
					setStatus("Logged In : " +pDialog.getFirstAndLastName(),"Click on \"Main Functions\" tab to LOG OUT");
					lastSelectedTab = 3;
					lastAccessLevel = pDialog.getAccessLevel();
				}
			}//END if	
			lastSelectedTab = 2;
			lastAccessLevel = pDialog.getAccessLevel();
		}
		
		if(mainTab.getSelectedIndex() == 3)
		{
			  System.out.println("last Index" + lastSelectedTab);
			  if(((lastSelectedTab != 4) || (lastSelectedTab != 3) || (lastSelectedTab != 1)|| (lastSelectedTab != 5)) && (lastAccessLevel != 3))
			  {
			  	    empBaseP.removeAll();
			  	    employeePanel = new EmpPanel(turtleConn);
			  	    empBaseP.add(employeePanel);
			  	    empBaseP.validate();
			  	    empBaseP.repaint();
					String dialogMsg = "      Please enter your password to gain access to Employees" ;
					       
					pDialog = new PasswordXXX(this,dialogMsg,turtleConn);  
					 
					if(pDialog.getLogonSuccess() == false)
					{
						mainTab.setSelectedIndex(0);
						setStatus("Logged In : ","");
					}
					else 
					if(employeePanel.getAccessLevel()  > pDialog.getAccessLevel())
					{
						JOptionPane.showMessageDialog(this,""+pDialog.getFirstAndLastName()+ " Does not have access to this area!");
						mainTab.setSelectedIndex(0);
						setStatus("Logged In : ","");
					}
					else
					{
						setStatus("Logged In : " +pDialog.getFirstAndLastName(),"Click on \"Main Functions\" tab to LOG OUT");
						lastSelectedTab = 3;
						lastAccessLevel = pDialog.getAccessLevel();
					}
			   }//end if check
			   else
			   {
					lastSelectedTab = 3;
					lastAccessLevel = pDialog.getAccessLevel();
			   } 		
		}//
		
		if(mainTab.getSelectedIndex() == 4)
		{
			mAreaPanel.setSelectedManagerAreaPanel();
		
			if(((lastSelectedTab != 3) || (lastSelectedTab != 2) || (lastSelectedTab != 1)|| (lastSelectedTab != 5)) && (lastAccessLevel != 3))
			{
				String dialogMsg = "   Please enter your password to gain access to managersArea" ;
				
				managerBaseP.removeAll();
			    mAreaPanel = new ManagersArea(turtleConn); 
			    managerBaseP.add(mAreaPanel);
			    managerBaseP.validate();       
			    managerBaseP.repaint();       
				pDialog = new PasswordXXX(this,dialogMsg,turtleConn);  
				 
				if(pDialog.getLogonSuccess() == false)
				{
					mainTab.setSelectedIndex(0);
					setStatus("Logged In : ","");
				}
				else 
				if(mAreaPanel.getPanelsAccess()  > pDialog.getAccessLevel())
				{
					JOptionPane.showMessageDialog(this,""+pDialog.getFirstAndLastName()+ "Does not have access to this area!");
					mainTab.setSelectedIndex(0);
					setStatus("Logged In : ","");
				}
				else
				{
					setStatus("Logged In : " +pDialog.getFirstAndLastName(),"Click on \"Main Functions\" tab to LOG OUT");
					if(pDialog.getAccessLevel() == 2)
					{
						mAreaPanel.setReportBut(false);
						lastAccessLevel = pDialog.getAccessLevel();
						lastSelectedTab = 4;
					}
					else
					{ 
						mAreaPanel.setReportBut(true);
						lastAccessLevel = pDialog.getAccessLevel();
						lastSelectedTab = 4;
					}
						
				}//end else
				lastSelectedTab = 4;
					
			}//end inner if
			else
			if(lastAccessLevel < 2)
			{
				JOptionPane.showMessageDialog(this,""+pDialog.getFirstAndLastName()+ "Does not have access to this area!");
				mainTab.setSelectedIndex(0);
				setStatus("Logged In : ","");
			}
			else
			if(lastAccessLevel <3)
			{
				mAreaPanel.setReportBut(false);
				
			}
			else mAreaPanel.setReportBut(true);
			
	        lastSelectedTab = 4;
	        System.out.println("lastSelectedTab = " + lastSelectedTab);
	        System.out.println("Last access level = " + lastAccessLevel);
		}
		
		if(mainTab.getSelectedIndex() == 5)
		{
			lastSelectedTab = 5;
			if(pDialog == null)
			{
				lastAccessLevel = 0;
			}
			else
			{
				lastAccessLevel = pDialog.getAccessLevel();
				System.out.println("dfffffffffffffLast access levels :"+ lastAccessLevel);
			}	
		}
	}
	
	class MyWindowListener extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			int choice = JOptionPane.showConfirmDialog(null,"Are you sure you wish to exit?","Confirmation",JOptionPane.YES_NO_OPTION);
			if(choice == JOptionPane.YES_OPTION)
			{
				System.out.println("Exiting..");
				msc.closeConn();
				System.exit(0);
			}
			else System.out.println("NO!!!!!");	
		}
	}
	public void setStatus(String s,String s2)
	{
		statusLabel.setText(s);
		instructLabel.setText(s2);
	}
	
	public static void main(String [ ] args)
	{
		new MainFrameGui();
	}

}
